package com.vhall.component.controller.interactive;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.vhall.component.controller.interactive.facade.QaConsoleFacade;
import com.vhall.component.entity.interactive.vo.QaExportReqVo;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.framework.common.exception.BusinessException;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.exception.BizErrorCode;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.interactive.QaConsoleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author xianglei
 * @date 2021/7/15 16:17
 */

@VhallComponent
@TokenAuth
public class QaConsoleController implements QaConsoleFacade {

    @Autowired
    private QaConsoleService consoleService;


    @Value("${paas.apps.lite.appId}")
    public String appId;

    @Override
    public Boolean create(QaExportReqVo reqVo) {
        try {
            if (StringUtils.isNotBlank(reqVo.getBeginTime())) {
                LocalDateTimeUtil.parse(reqVo.getBeginTime(), DatePattern.NORM_DATE_PATTERN);
            }
            if (StringUtils.isNotBlank(reqVo.getEndTime())) {
                LocalDateTimeUtil.parse(reqVo.getEndTime(), DatePattern.NORM_DATE_PATTERN);
            }
        } catch (Exception e) {
            throw new BusinessException(BizErrorCode.BIZ_INVALID_TIME);
        }
        String roomId = reqVo.getRoomId();
        Token token = TokenThreadLocal.getToken();
        Integer accountId = token.getAccountId();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
        String time = sdf.format(new Date());
        String fileName = time + "问答记录" + time;
        return consoleService.create(fileName, appId, roomId, accountId, reqVo.getBeginTime(), reqVo.getEndTime());
    }
}
